<?php

class CategoryController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
    }

    public function indexAction() {
        $this->_helper->redirector('list', 'category', '');
    }

    public function listAction() {

        $errorMessage = Array();
        $successmessage = "";
        $where = "";
        $searchtype = "";
        $searchtext = "";
        $searchunit = "";
        $searchstatus = "";
        $orderField = 'cat_id';
        $sort = 'asc';

        /* Pagination Login */
        $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
        $currentPageReviews = 1;

        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }


        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

        if ($this->_request->isPost()) {
            $postedData = $this->_request->getPost();

            /* Search Facility Over Here */
            if (isset($postedData["searchtype"]) && ($postedData["searchtype"] != "")) {
                $searchtype = $postedData["searchtype"];
                if ($searchtype == "cat_name") {
                    $searchtext = $postedData["searchtext"];
                    if ($searchtext == '') {
                        $errorMessage[] = "Please enter search text for search.";
                    } else {
                        $where = "$searchtype LIKE '%$searchtext%'";
                    }
                }
            }
        }

        $categoryModel = new Model_Category();
        $categorysData = $categoryModel->fetchCategorys($where, $itemsPerPageReviews, $offset, $orderField, $sort);

        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        $paginator->setItemsTotal($categoryModel->getCategoryCount($where));
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'category', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

        $this->view->paginator = $paginator;
        $this->view->categorysData = $categorysData;

        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;

        $this->view->catArray = $categoryModel->getCategoryArray();
        $this->view->searchtype = $searchtype;
        $this->view->searchtext = $searchtext;
        $this->view->searchunit = $searchunit;
        $this->view->searchstatus = $searchstatus;


        $indexUrl = $this->view->url(array('controller' => 'index', 'action' => 'index'), null, TRUE);
        $categoryUrl = $this->view->url(array('controller' => 'category', 'action' => 'list'), null, TRUE);
        $this->view->breadcrumbs = array($indexUrl => "Home", $categoryUrl => "Category", "" => "Category Listing");
    }

    public function deleteAction($ids = Array()) {
        //Load Category Model 
        $categoryModel = new Model_Category();

        if ($this->_getParam('cat_id') && $this->_getParam('cat_id') != "") {
            $category_id = $this->_getParam('cat_id');
            
            $categoryArray = $categoryModel->fetchCategorys("cat_parentid = '".$category_id."'");
            if ( count($categoryArray) > 0 ) {
                $this->addErrorMessage("Category is already in use!");
            } else {
                $this->addSuccessMessage("Category deleted succesfully");
                $categoryModel->deleteCategory($category_id);
            }
            $this->_helper->redirector('list', 'category', '');
        }
    }

    public function formAction() {

        //Initialize
        $successmessage = "";
        $errorMessageArray = Array();
        $data = array();
        $categoryData = Array();
        $this->view->mode = "Add";

        //Load Category Model
        $categoryModel = new Model_Category();

        //Load Category Form
        $categoryForm = new Form_CategoryForm();


        //Load the unit array for the select box in form 
        $catArray = $categoryModel->getCategoryArray();

        //Check for the category Parameter
        $categoryData['cat_id'] = $this->_request->getParam('cat_id');
        $cat_id = (!empty($categoryData['cat_id'])) ? $categoryData['cat_id'] : null;

        if (!empty($cat_id)) {
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            //Fetch data to be loaded in form 
            $data = $categoryModel->fetchEntryById($cat_id);
            unset($catArray[$cat_id]);
        }

        $categoryForm->cat_parentid->addMultiOptions($catArray);
        //Populate form 
        $this->view->form = $categoryForm->populate($data);

        if ($this->getRequest()->isPost()) {

            //Check for Errors
            $postedData = $this->_request->getPost();
            if (!$categoryForm->isValid($postedData)) {
                $errorMessage = $categoryForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {
                if (count($errorMessageArray) > 0) {
                    
                } else {
                    //Category save
                    $categoryData['cat_id'] = $cat_id;
                    $categoryData['cat_name'] = $postedData['cat_name'];
                    $categoryData['cat_parentid'] = $postedData['cat_parentid'];
                    $categoryModel->save($categoryData, 'cat_id');
                    if ($this->view->mode == "Add") {
                        $successmessage = "Category added successfully";
                    } else {
                        $successmessage = "Category updated successfully";
                    }
                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('form', 'category', '');
                }
            }
        }

        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $categoryForm;
        $indexUrl = $this->view->url(array('controller' => 'index', 'action' => 'index'), null, TRUE);
        $categoryUrl = $this->view->url(array('controller' => 'category', 'action' => 'list'), null, TRUE);
        $this->view->breadcrumbs = array($indexUrl => "Home", $categoryUrl => "Category", "" => $this->view->mode . " Category");
    }

}

?>